/*
 * @Author: Li Zengkun
 * @Date: 2022-09-09 13:41:14
 * @LastEditors: Li Zengkun
 * @LastEditTime: 2022-09-26 13:42:24
 * @Description: 
 */
import { createRouter, createWebHashHistory } from 'vue-router'
import { ElMessage } from "element-plus"

import Layout from '@/components/Layout.vue'
import UserLayout from '@/components/FrontEnd/Layout.vue'

const router = createRouter({
    history: createWebHashHistory(),
    routes: [{
            path: '/login',
            alias: '/',
            name: 'Login',
            component: () =>
                import ('@/views/Login.vue')
        },
        {
            path: '/register',
            name: 'Register',
            component: () =>
                import ('@/views/Register.vue')
        },
        {
            path: '/user/login',
            name: 'UserLogin',
            component: () =>
                import ('@/views/FrontEnd/Login.vue')
        },
        {
            path: '/user/register',
            name: 'UserRegister',
            component: () =>
                import ('@/views/FrontEnd/Register.vue')
        },
        {
            path: '/front-end',
            name: 'FrontEnd',
            component: UserLayout,
            meta: { requiresAuth: true },
            children: [{
                    path: "home",
                    name: "UserHome",
                    component: () =>
                        import ('@/views/FrontEnd/Home.vue')
                },
                {
                    path: "article:id",
                    name: "UserArticle",
                    component: () =>
                        import ('@/views/FrontEnd/Article.vue')
                },
                {
                    path: "notice:id",
                    name: "UserNotice",
                    component: () =>
                        import ('@/views/FrontEnd/Notice.vue')
                },
                {
                    path: "center/:id",
                    name: "UserCenter",
                    component: () =>
                        import ('@/views/FrontEnd/Center.vue')
                },
                {
                    path: "center/:id",
                    name: "UserCenter",
                    component: () =>
                        import ('@/views/FrontEnd/Center.vue')
                },
                {
                    path: "settings",
                    name: "UserSettings",
                    component: () =>
                        import ('@/views/FrontEnd/UserSettings.vue')
                },
            ]

        },
        // {
        //     path: '/test',
        //     name: 'Test',
        //     component: Layout,
        //     children: [{
        //         path: '',
        //         component: () =>
        //             import ('@/views/Test.vue'),
        //     }]
        // },
        {
            path: '/admin',
            name: 'Admin',
            meta: { requiresAuth: true },
            component: Layout,
            children: [{
                    path: "",
                    alias: 'home',
                    name: 'AdminHome',
                    component: () =>
                        import ("@/views/Admin/Home.vue")
                },
                {
                    path: 'list',
                    name: "AdminList",
                    component: () =>
                        import ('@/views/Admin/List.vue'),
                },
                {
                    path: 'info',
                    name: "AdminInfo",
                    component: () =>
                        import ('@/views/Admin/Info.vue'),
                },
                {
                    path: "edit",
                    name: 'AdminEdit',
                    component: () =>
                        import ("@/views/Admin/Edit.vue")
                },
            ]
        },
        {
            path: '/article',
            name: "Article",
            meta: { requiresAuth: true },
            component: Layout,
            children: [{
                    path: "release",
                    name: 'ArticleRelease',
                    component: () =>
                        import ("@/views/Article/Release.vue")
                },
                {
                    path: "list",
                    name: 'ArticleList',
                    component: () =>
                        import ("@/views/Article/List.vue")
                },
                {
                    path: "edit",
                    name: 'ArticleEdit',
                    component: () =>
                        import ("@/views/Article/Edit.vue")
                },
                {
                    path: "detail",
                    name: 'ArticleDetail',
                    component: () =>
                        import ("@/views/Article/Detail.vue")
                },
            ],
        },
        {
            path: '/tag',
            name: "Tag",
            meta: { requiresAuth: true },
            component: Layout,
            children: [{
                path: "list",
                name: 'TagList',
                component: () =>
                    import ("@/views/Tag/List.vue")
            }, ],
        },
        {
            path: '/comment',
            name: "Comment",
            meta: { requiresAuth: true },
            component: Layout,
            children: [{
                path: "list",
                name: 'CommentList',
                component: () =>
                    import ("@/views/Comment/List.vue")
            }, ],
        },
        {
            path: '/category',
            name: "Category",
            meta: { requiresAuth: true },
            component: Layout,
            children: [{
                path: "list",
                name: 'CategoryList',
                component: () =>
                    import ("@/views/Category/List.vue")
            }, ],
        },
        {
            path: '/notice',
            name: "Notice",
            meta: { requiresAuth: true },
            component: Layout,
            children: [{
                    path: "release",
                    name: 'NoticeRelease',
                    component: () =>
                        import ("@/views/Notice/Release.vue")
                },
                {
                    path: "list",
                    name: 'NoticeList',
                    component: () =>
                        import ("@/views/Notice/List.vue")
                },
                {
                    path: "detail/:id",
                    name: 'NoticeDetail',
                    component: () =>
                        import ("@/views/Notice/Detail.vue")
                },
                {
                    path: "edit/:id",
                    name: 'NoticeEdit',
                    component: () =>
                        import ("@/views/Notice/Edit.vue")
                },
            ],
        },
        {
            path: '/user',
            name: "User",
            meta: { requiresAuth: true },
            component: Layout,
            children: [{
                path: "list",
                name: 'UserList',
                component: () =>
                    import ("@/views/User/List.vue")
            }, ],
        },
        {
            path: '/link',
            name: "Link",
            meta: { requiresAuth: true },
            component: Layout,
            children: [{
                path: "list",
                name: 'LinkList',
                component: () =>
                    import ("@/views/Link/List.vue")
            }, ],
        },
        {
            path: '/slide',
            name: "Slide",
            meta: { requiresAuth: true },
            component: Layout,
            children: [{
                path: "list",
                name: 'SlideList',
                component: () =>
                    import ("@/views/Slide/List.vue")
            }, ],
        },
        {
            path: '/auth',
            name: "Auth",
            meta: { requiresAuth: true },
            component: Layout,
            children: [{
                    path: "role",
                    name: 'AuthRole',
                    component: () =>
                        import ("@/views/Auth/Role.vue")
                },
                {
                    path: "menu",
                    name: 'AuthMenu',
                    component: () =>
                        import ("@/views/Auth/Menu.vue")
                },
            ],
        },
    ]
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
    // 修改标题
    document.title = to.name

    // 检查目标地址，是否需要登录
    let isRequireAuth = to.matched.some(item => item.meta.requiresAuth)

    // 不需要登陆
    if (!isRequireAuth) {
        next()
        return
    }
    // 需要登陆，获取token
    let { token } = sessionStorage
    // token 不存在，则跳转到登陆页面，提示用户原因
    if (!token) {
        ElMessage({
            type: "error",
            message: '请登录！'
        })
        let reg = /front/
        let isFront = reg.test(to.path)
        if (isFront) {
            next({ name: 'UserLogin' })

        } else {
            next({ name: 'Login' })
        }
        return
    }
    // token 存在，放行
    next()
})

export default router